package com.example.bigwork.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf(csrf -> csrf.disable()) // Disable CSRF for stateless APIs if not using browser sessions
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // Stateless sessions
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/api/users/register", "/api/users/login","/api/order").permitAll() // Allow public access to register and login
                        .requestMatchers("/api/products/**").permitAll() // Allow public access to view products
                        // .requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() // If using OpenAPI/Swagger
                        .anyRequest().authenticated() // All other requests require authentication (placeholder, adjust as needed)
                );
        // If you implement JWT, you would add a JWT filter here.
        // For this simplified version, we rely on service-level checks for non-public endpoints after login.
        // A full JWT setup would be more robust for securing endpoints.
        return http.build();
    }
}
